package org.eenie.qeasy;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

public class AddConfigActivity extends AppCompatActivity implements View.OnClickListener, BaseQuickAdapter.OnItemLongClickListener {
    EditText mEtGap;
    EditText mEtDura;
    Spinner mSpType;
    Spinner mSpStro;
    Button mBtnAdd;
    RecyclerView mRvConfig;

    ConfigAdapter mConfigAdapter;

    List<PulseConfig> mPulseConfigs = new ArrayList<>();

    private int[] colors = new int[]{
            Color.parseColor("#50BB0B6F"),
            Color.parseColor("#505FBB2D"),
            Color.parseColor("#50BBA025"),
            Color.parseColor("#503549FF"),
            Color.parseColor("#50FF133A"),
            Color.parseColor("#5016FFE1"),
            Color.parseColor("#50FFAB00")};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_config);
        initView();
    }

    private void initView() {
        mEtGap = findViewById(R.id.edit_gap);
        mEtDura = findViewById(R.id.edit_duration);
        mSpType = findViewById(R.id.sp_type);
        mSpStro = findViewById(R.id.sp_sto);
        mBtnAdd = findViewById(R.id.btn_add);
        mRvConfig = findViewById(R.id.rv_config);

        mBtnAdd.setOnClickListener(this);

        mRvConfig.setLayoutManager(new LinearLayoutManager(this));
        mRvConfig.setAdapter(mConfigAdapter = new ConfigAdapter(mPulseConfigs));

        mConfigAdapter.setOnItemLongClickListener(this);
        mConfigAdapter.openLoadAnimation();


    }

    @Override
    public void onClick(View view) {
        mConfigAdapter.addData(0,zipConfig());
    }


    private PulseConfig zipConfig() {
        StringBuilder cmd = new StringBuilder();
        cmd.append("0807");
        cmd.append("0" + (mSpType.getSelectedItemPosition() + 1));
        cmd.append("0" + (mSpStro.getSelectedItemPosition() + 1));
        cmd.append("1480");
        return new PulseConfig(fetchTotalTime() + Integer.valueOf(mEtGap.getText().toString()), Integer.valueOf(mEtDura.getText().toString()), cmd.toString(), colors[mSpType.getSelectedItemPosition()]);
    }



    public int fetchTotalTime() {
        int t = 0;
        List<PulseConfig> pulseConfigs = mConfigAdapter.getData();

        if (pulseConfigs.isEmpty()) {
            return t;
        } else {
            t = (int) (pulseConfigs.get(0).startTime + pulseConfigs.get(0).duration);
            return t;
        }
    }


    @Override
    public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
        mConfigAdapter.remove(position);
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add("确定").setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        setResult( Activity.RESULT_OK,getIntent().putParcelableArrayListExtra("data", (ArrayList<? extends Parcelable>) mConfigAdapter.getData()));
        finish();
        return super.onOptionsItemSelected(item);
    }
}
